//
//  ByNetBaseResponse.m
//  jmsfhz_ios
//
//  Created by pro on 16/8/10.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByNetBaseResponse.h"

/** 原始数据返回的type为E */
static NSString *errorWithTypeDesc = @"服务器异常!请重试";
static NSInteger errorWithTypeCode = -1001;


@implementation ByNetBaseResponse

- (id)initWithDic:(NSDictionary *)dic trueType:(ByNetDataType)trueType {
    
    if (self = [super init]) {
        self.client_id = dic[@"client_id"];
        self.notify_id = [NSString stringWithFormat:@"%@", dic[@"notify_id"]];
        self.sign = dic[@"sign"];
        self.time = [NSString stringWithFormat:@"%@", dic[@"time"]];
        self.type = dic[@"type"];
        self.data = dic[@"data"];
        
        self.trueType = trueType;
    }
    return self;
}

- (void)dataAnalysis {
    
    /** 首先判断数据type是否正确，T表示正确，E表示错误 */
    if ([self.type isEqualToString:@"E"]) {
        self.failourBlock(errorWithTypeDesc, errorWithTypeCode);
        return;
    }
    
    /** 再对加密数据进行解密，并判断正误 */
    NSString *encryptedData = self.data;                                // 加密的数据
    NSDictionary *dataDic = [ByCoreCrypt decryptString:encryptedData];  // 解密后的数据字典，包含code，data，notify_id
    
    self.dataModel = [[ByNetBaseResponseData alloc] initWithDic:dataDic trueType:self.trueType];
    __weak typeof(self) weakSelf = self;
    [self.dataModel setFailourBlock:^(NSString *errorDesc, NSInteger code) {
        weakSelf.failourBlock(errorDesc, code);
    }];
    [self.dataModel setSuccessBlock:^(id data) {
        weakSelf.successBlock(data, weakSelf.notify_id);
    }];
    [self.dataModel dataAnalysis];
    
    


}


@end
